package com.ysg.widget.pic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.utils.DateUtils;
import com.ysg.R;
import com.ysg.utils.YFileUtil;
import com.ysg.utils.YImageUtil;
import java.util.ArrayList;
import java.util.List;


public class SelectPicAdapter extends RecyclerView.Adapter<SelectPicAdapter.ViewHolder> {
    public static final int TYPE_ADD = 1;
    public static final int TYPE_PICTURE = 2;
    private final List<SelectPicBean> list = new ArrayList<>();
    private int selectMax = 9;
    private Context context;
    private SelectPicEnum selectPicEnum = SelectPicEnum.select;

    public SelectPicAdapter(Context context) {
        this.context = context;
    }

    public SelectPicAdapter(Context context, SelectPicEnum selectPicEnum) {
        this.context = context;
        this.selectPicEnum = selectPicEnum;
    }

    public int getSelectMax() {
        return selectMax;
    }

    public List<SelectPicBean> getData() {
        return list;
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView mIvPic;
        ImageView mIvDel;
        ImageView ivVideo;
        public ViewHolder(View view) {
            super(view);
            mIvPic = view.findViewById(R.id.iv_pic);
            mIvDel = view.findViewById(R.id.iv_del);
            ivVideo = view.findViewById(R.id.ivVideo);
        }
    }

    @Override
    public int getItemCount() {
        if (list.size() < selectMax && SelectPicEnum.select == selectPicEnum) {
            return list.size() + 1;
        } else {
            return list.size();
        }
    }

    @Override
    public int getItemViewType(int position) {
        int size = list.size();
        if (position == size) {
            return TYPE_ADD;
        } else {
            return TYPE_PICTURE;
        }
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_select_pic, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
        //少于MaxSize张，显示继续添加的图标
        if (getItemViewType(position) == TYPE_ADD) {
            viewHolder.mIvDel.setVisibility(View.INVISIBLE);
            viewHolder.mIvPic.setImageResource(R.mipmap.ic_pic_add);
            viewHolder.mIvPic.setOnClickListener((v)->{
                if (listener != null) {
                    listener.onAddPicListener();
                }
            });

        } else {
            //删除
            if (SelectPicEnum.select == selectPicEnum){
                viewHolder.mIvDel.setVisibility(View.VISIBLE);
                viewHolder.mIvDel.setOnClickListener(view -> {
                    int index = viewHolder.getAbsoluteAdapterPosition();
                    if (index != RecyclerView.NO_POSITION && list.size() > index) {
                        if (listener != null) {
                            listener.onDeletePicListener(index);
                        }
                        list.remove(index);
                        notifyItemRemoved(index);
                        notifyItemRangeChanged(index, list.size());
                    }
                });
            }

            //itemView 的点击事件
            viewHolder.itemView.setOnClickListener(v -> {
                int adapterPosition = viewHolder.getAbsoluteAdapterPosition();
                zoomPic(adapterPosition);
            });

            SelectPicBean item = list.get(position);
            YImageUtil.show(viewHolder.mIvPic, item.getMinPic());

            if (YFileUtil.isVideo(item.getPic())){
                viewHolder.ivVideo.setVisibility(View.VISIBLE);
            }else{
                viewHolder.ivVideo.setVisibility(View.GONE);
            }

        }
    }

    private OnSelectPicListener listener;
    public void setOnSelectListener(OnSelectPicListener l) {
        this.listener = l;
    }

    public interface OnSelectPicListener {
        void onAddPicListener();
        void onDeletePicListener(int position);
    }

    public void zoomPic(int position){
        ArrayList<LocalMedia> localMedias = new ArrayList<>();
        for (SelectPicBean item: list) {
            LocalMedia media = new LocalMedia();
            if (YFileUtil.isVideo(item.getPic())){
                media.setMimeType("video/mp4");
                media.setRealPath(item.getPic());
            }
            media.setPath(item.getPic());
            localMedias.add(media);
        }
        YPicUtils.getInstance().zoomPic(context, localMedias, position);
    }

}
